﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entities;

namespace WebUI.Areas.Reports.Binders
{
    public class FilterOperationsBinder : DefaultModelBinder, IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var filterOperations = (FilterOperations)base.BindModel(controllerContext, bindingContext);

            filterOperations.FileTypes = filterOperations.FileTypes == null || !filterOperations.FileTypes.Any() || filterOperations.FileTypes.Single().Length == 0 ? new string[] { } : filterOperations.FileTypes.Single().Split(new char[] { ',' });
            filterOperations.Hosts = filterOperations.Hosts.Single().Length != 0 ? filterOperations.Hosts.Single().Split(new char[] { ',' }) : new string[] { };
            filterOperations.Policies = filterOperations.Policies.Single().Length != 0 ? filterOperations.Policies.Single().Split(new char[] { ',' }) : new string[] { };
            string operationTypes = controllerContext.HttpContext.Request.Form["OperationTypes"];

            filterOperations.OperationTypes = string.IsNullOrEmpty(operationTypes) ? new int[] { } : operationTypes.Split(new char[] { ',' }).Select(o => int.Parse(o));


            return filterOperations;
        }
    }
}